package com.jiesen.base.modules.login.jsPermission.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiesen.base.modules.login.jsPermission.model.Permission;
import com.jiesen.base.modules.login.jsPermission.service.PermissionService;
import com.jiesen.base.modules.login.jsPermission.utils.PermissionUtils;
import com.jiesen.base.utils.R;
import com.jiesen.base.utils.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/login/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    //增加一个权限信息
    @PostMapping("/create")
    public R createPermission(@RequestBody Permission permission) {
        //格式化权限信息
        permission = PermissionUtils.permissionComplete(permission);
        if(permission==null){
            return R.error("没有名称");
        }
        //新增时间
        permission.setAddtime(new Date());
        boolean save = permissionService.save(permission);
        if (!save) {
            return R.error();
        }
        return R.ok();
    }

    //删除一个权限信息
    @GetMapping("/delete")
    public R deletePermission(@RequestParam("id")Integer id){
        boolean remove = permissionService.removeById(id);
        if(!remove){
            return R.error();
        }
        return R.ok();
    }

    //根据id修改权限信息
    @PostMapping("/edit")
    public R editPermission(@RequestBody Permission permission){
        if(permission.getId()==null){
            return R.error("没有id信息");
        }
        //格式化权限信息
        permission = PermissionUtils.permissionComplete(permission);
        if(permission==null){
            return R.error("没有名称");
        }
        //增加修改时间
        permission.setUpdatetime(new Date());

        boolean b = permissionService.updateById(permission);
        if(!b){
            return R.error();
        }
        return R.ok();
    }

    //根据id查询权限信息
    @GetMapping("/view")
    public R viewPermission(@RequestParam("id")Integer id){
        Permission byId = permissionService.getById(id);
        return R.ok().put("data",byId);
    }

    //查询所有权限
    @GetMapping("/viewTree")
    public R viewTreePermission(){
        List<Permission> permissions =  permissionService.findAll();
        JSONArray a = TreeUtil.listToTree(JSONArray.parseArray(JSON.toJSONString(permissions)), "id", "parentId", "child");
        return R.ok().put("data",a);
    }

    //根据父类id查询权限信息
    @GetMapping("/viewParent")
    public R viewpermission(@RequestParam("parentId")Integer id){
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        return R.ok().put("data",permissionService.list(queryWrapper));
    }

    //根据条件查询权限信息
    @PostMapping("/view")
    public R viewPermission(){
        List<Permission> all = permissionService.findAll();
        return R.ok().put("data",all);
    }

}